﻿using System;
using System.Collections.Generic;
using System.Data.Odbc;
using System.Linq;
using System.Text;
using Remotion.Linq.Parsing.Structure;
using Remotion.Linq;

namespace CITPRO.Linq.LinqToOdbc {
    public class OdbcQueryContext : IOdbcQueryContext {
        private readonly IOdbcQueryExecutor queryExecutor;
        private readonly OdbcConnection connection;

        public OdbcQueryContext(IOdbcQueryExecutor queryExecutor, IOdbcConnectionProvider connectionProvider) {
            connection = connectionProvider.GetConnection();
            connection.Open();
            this.queryExecutor = queryExecutor;
            queryExecutor.SetConnection(connection);
        }

        public OdbcQueryable<T> Queryable<T>() {
            return new OdbcQueryable<T>(QueryParser.CreateDefault(), queryExecutor);
        }

        public void Dispose() {
            connection.Close();
            connection.Dispose();
            queryExecutor.Dispose();
        }
    }
}
